﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Project.Model.Rights;
using System.Linq;
using Project.Model;
using Microsoft.Office.Interop.Word;
using System.Data;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Text;
using Project.Common;
using System.Net;
using NHibernate.Criterion;

namespace Web
{
    /// <summary>
    /// 查找所有受理中心审核通过的项目,逐个转换成 pdf
    /// </summary>
    public partial class _ExportRWSAll : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tempStr = string.Empty;

            // 查找需要转换的项目
            //创建查询器
            IList<ICriterion> queryList = new List<ICriterion>();

            //queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Checkqk), "3.2"));
            //IList<ShqUserManager> list = ManagerFactory.ShqUserManagerManager.GetList(queryList);


            queryList.Add(Restrictions.IsNotNull(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Itemid)));
            IList<HTTable> list = ManagerFactory.HTTableManager.GetList(queryList);
            ResponseWrite("共需要转换: "+list.Count+ " 项<br />\r\n");
            for (int i = 0; i < list.Count; i++)
            {
                HTTable item = list[i];

                Itemtable itemTable = ManagerFactory.ItemtableManager.Get(int.Parse(item.Itemid));
                if (itemTable == null)
                {
                    ResponseWrite(i + " 合同项目  ID:" + item.Id + " 对应的 Itemtable 不存在<br />\r\n");
                    continue;
                }

                queryList = new List<ICriterion>();
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Indexbasictable>(d => d.Jhname), itemTable.Jhname));
                //queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Indexbasictable>(d => d.Jhyear), itemTable.Jhyear));
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Indexbasictable>(d => d.Itemname), itemTable.Itemname));
                //queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Indexbasictable>(d => d.Itempeople), itemTable.Itempeople));

                IList<Indexbasictable> listIndexbasictable = ManagerFactory.IndexbasictableManager.GetList(queryList);
                if (listIndexbasictable.Count == 1)
                {
                    Indexbasictable model = listIndexbasictable[0];
                    
                    // 检查是否需要转换
                    //string pdfCNPath = "~/@Export/" + model.Itemname + "-项目申报书-" + model.Id + ".pdf";
                    string pdfCNPath = "~/@Export/" + model.Id + "-" + fliterWindowName(model.Itemname) + "-项目合同书.pdf";
                    if (File.Exists(Server.MapPath(pdfCNPath)))
                    { 
                        ResponseWrite(i + " 项目  ID:" + model.Id + " 名称:" + model.Itemname + "   转换结果:" + "未转换.因为 PDF 已经存在" + "<br />\r\n");
                    }
                    //else if (!model.PdfExportDate.HasValue)
                    //else if (true)
                    else 
                    {
                        _ExportRWS page = new _ExportRWS();
                        page.PId = model.Id;
                        page.IsBatPDF = true;
                        page.BatPDFResult = "转换成功";
                        page.Page_Load(sender, e);
                        ResponseWrite(i + " 项目  ID:" + model.Id + " 名称:" + model.Itemname + "   转换结果:" + page.BatPDFResult + "<br />\r\n");
                    }
                    //else
                    //{ 
                    //    ResponseWrite(i + " 项目  ID:" + model.Id + " 名称:" + model.Itemname + "   转换结果:" + "已经转换过.上次转换时间:" + model.PdfExportDate.Value + "<br />\r\n");
                    //}

                }
                else
                {
                    ResponseWrite(i + " 项目 : " + item.Itemid + " :  在 Indexbasictable 表中对于的记录为" + listIndexbasictable.Count + "条!" + "<br />\r\n");
                }
                Response.Flush();
            }

        }

        /// <summary>
        /// 去除 windows 命名规则中不能有的字符 \/:*? " <> |
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private string fliterWindowName(string p)
        {
            string str = "\\/:*?\"<>|";
            for (int i = 0; i < str.Length; i++)
            {
                p = p.Replace(str[i].ToString(), "");
            }
            return p;
        }


        private void ResponseWrite(string p)
        {
            Response.Write(DateTime.Now.ToString() + "  " + p);
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            string result = string.Empty;
            result += KillPrecess("winword");
            result += KillPrecess("acrodist");
            ResponseWrite(result);
            Response.Flush();
        }
        /// <summary>
        /// 为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
        /// 参考页面 http://blog.csdn.net/hack8/archive/2009/02/05/3863483.aspx
        /// </summary>
        /// <param name="processName"></param>
        public string KillPrecess(string processName)
        {
            string result = string.Empty;
            //定义进程
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            try
            {
                //获取选择的进程名称
                string proname = processName;
                //从所有的进程中获取选择的进程
                foreach (Process pro in Process.GetProcessesByName(proname))
                {
                    //判断是否是系统的主要进程，如果是则不能结束
                    if (!pro.CloseMainWindow())
                    {
                        result += pro.ProcessName + " 进程被结束！<br />\r\n";
                        //结束进程
                        pro.Kill();
                    }
                }
            }
            catch (Exception err)
            {
                throw new System.Exception(err.Message);
            }
            return result;
        }


    }
}